
/*
 *
 * Copyright (C) 2008 Mattias Blomqvist, patr-blo at dsv dot su dot se
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using FlowLib.Containers;

namespace ClientExample.Controls
{
    public partial class HubSettings : UserControl
    {
        protected HubSetting setting = null;

        public HubSetting Settings
        {
            get
            {
                if (setting != null)
                {
                    setting.Name = txtName.Text;
                    setting.Address = txtIP.Text;
                    setting.Port = decimal.ToInt32(updwnPort.Value);
                    setting.Protocol = txtProtocol.Text;
                    setting.Set("Connect", (chkConnect.Checked ? "1" : "0"));
                    setting.DisplayName = txtUser.Text;
                    setting.Password = txtPassword.Text;
                }
                return setting;
            }
            set
            {
                setting = value;
                UpdateDisplay();
            }
        }

        public HubSettings()
        {
			IntPtr h = Handle;  // This is for stupid mono stuff. Some how handle isnt created correctly if we dont do this :(
            InitializeComponent();
            txtIP.TextChanged += new EventHandler(txtIP_TextChanged);
            BackgroundImageChanged += new EventHandler(HubSettings_BackgroundImageChanged);
        }

        void HubSettings_BackgroundImageChanged(object sender, EventArgs e)
        {
            if (this.BackgroundImage != null && !Containers.AppSetting.IsRunningOnMono)
            {
                Bitmap bigMap = (Bitmap)this.BackgroundImage;
                for (int i = 0; i < this.Controls.Count; i++)
                {
                    Control control = this.Controls[i];
                    if (control is Button || control is TextBox)
                        continue;
                    Bitmap background = new Bitmap(control.Width, control.Height);
                    Graphics graphics = Graphics.FromImage(background);
                    graphics.DrawImage(bigMap, new Point(0 - control.Left, 0 - control.Top));
                    control.BackgroundImage = background;
                    control.BackgroundImageLayout = ImageLayout.Stretch;
                    graphics.Dispose();
                }
            }
        }

        void txtIP_TextChanged(object sender, EventArgs e)
        {
            // TODO: Make this IPv6 compatible
            int pos =0;
            string strUrl = txtIP.Text;
            int port = 0;
            bool changedProtocol = false;
            // Remove protocol name from addy
            if ((pos = strUrl.IndexOf("://")) != -1)
            {
                string protocolName = strUrl.Substring(0, pos).ToLower();
                switch (protocolName)
                {
                    case "adc":
                        txtProtocol.Text = "Adc";
                        break;
                    case "adcs":
                        txtProtocol.Text = "AdcSecure";
                        break;
                    case "nmdc":
                    case "dchub":
                        txtProtocol.Text = "Nmdc";
                        break;
                }
                pos += 3;
                strUrl = strUrl.Substring(pos);
                changedProtocol = true;
            }

            // Remove port from addy
            if ((pos = strUrl.LastIndexOf(':')) > 0)
            {
                string strPort = strUrl.Substring(pos + 1);
                strUrl = strUrl.Substring(0, pos);
                if (int.TryParse(strPort, out port))
                {
                    updwnPort.Value = new decimal(port);
                }
            }

            if (port > 0 || changedProtocol)
            {
                txtIP.Text = strUrl;
                // If text length is zero will set hub name :)
                if (txtName.TextLength == 0)
                {
                    txtName.Text = txtIP.Text;
                }
                // We will now shift focus to user name
                txtUser.Focus();
            }
        }

        protected void UpdateDisplay()
        {
            if (setting == null)
                setting = new HubSetting();
            txtName.Text = setting.Name;
            updwnPort.Value = (decimal)setting.Port;
            txtProtocol.Text = setting.Protocol;
            txtIP.Text = setting.Address;
            chkConnect.Checked = (setting.ContainsKey("Connect") && "1".Equals(setting.Get("Connect")));
            txtUser.Text = setting.DisplayName;
            txtPassword.Text = setting.Password;
        }

        private void HubSettings_Load(object sender, EventArgs e)
        {
            UpdateDisplay();
        }
    }
}
